package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.work.impl.dao.ProductionReportDao;
import com.jiantong.work.impl.manager.ProductionReportManager;
import com.jiantong.work.model.ProductionReport;
import com.jiantong.work.service.ProductionReportWriteService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Created by niufei on 2017/3/14.
 *
 */
@Slf4j
@Service
@RpcProvider
public class ProductionReportWriteServiceImpl implements ProductionReportWriteService{

    private final ProductionReportManager productionReportManager;

    private final ProductionReportDao productionReportDao;

    @Autowired
    public ProductionReportWriteServiceImpl(ProductionReportManager productionReportManager,ProductionReportDao productionReportDao) {

        this.productionReportManager = productionReportManager;
        this.productionReportDao=productionReportDao;
    }

    /**
     * 新建一条单据
     * @param productionReport 生产记录对象
     * @return 返回该单据的id
     */
       @Override
    public Response<Map<String,Object>> create(ProductionReport productionReport,Integer toStatus) {
           try {
               return Response.ok(productionReportManager.createProductionReport(productionReport,toStatus));
           }catch (Exception e){
               log.error("failed to create productionReport error :{}", Throwables.getStackTraceAsString(e));
               return Response.fail("create.failed");
           }
       }

    /**
     * 更新一条单据
     * @param productionReport 生产记录对象
     * @return 返回true or false
     */
    @Override
    public Response<Map<String,Object>> update(ProductionReport productionReport,Integer toStatus) {
        try {
            return Response.ok(productionReportManager.updateProductionReport(productionReport,toStatus));
        }catch (Exception e){
            log.error("failed.to.update {},case:{}" ,productionReport,Throwables.getStackTraceAsString(e));
            return Response.fail("production.report.update.fail");
        }
    }

    @Override
    public Boolean update(ProductionReport productionReport) {
        return productionReportDao.update(productionReport);
    }
}
